package com.xgzit.xgzmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * 跨域配置
 * @author xgz
 * @date 2021-03-23 18:45
 */
@Configuration
public class XgzMallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        // UrlBasedCorsConfigurationSource 是 CorsConfigurationSource 的一个实现类
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 配置跨域
        /**
         * 运行跨域的 请求头
         **/
        corsConfiguration.addAllowedHeader("*");
        /**
         * 运行跨域的 方法
         **/
        corsConfiguration.addAllowedMethod("*");
        /**
         * 运行跨域的 源
         **/
        corsConfiguration.addAllowedOrigin("*");
        /**
         * 运行跨域携带cookie
         **/
        corsConfiguration.setAllowCredentials(true);
        /**
         * 配置那些路径需要跨域
         **/
        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsWebFilter(source);
    }
}
